package com.rrd.dw.mr.gzip.kafka;

import java.util.ArrayList;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.tongdun.TongDunRiskRecord;
import com.ucredit.babel.common.model.third.tongdun.details.*;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.List;

/**
 * 同盾-命中规则详情查询多头&逾期
 *
 * @author guoqiang
 */

public class TongDunRiskHitDimV2Parse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 同盾-命中规则详情查询多头&逾期
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        TongDunRiskRecord obj = (TongDunRiskRecord) model;
        TongDunDetailsResult risk_obj = obj.getHit_rule_result() == null ? new TongDunDetailsResult() : obj.getHit_rule_result();
        StringBuilder sb = getBaseInfo(obj, successful);
        sb.append(formatValue(risk_obj.isSuccess())).append(CRT001);
        sb.append(formatValue(risk_obj.getReason_code())).append(CRT001);
        sb.append(formatValue(risk_obj.getReason_desc())).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);

        StringBuilder TxSb = new StringBuilder(500);
        List<TongDunDetailsRule>  rules = risk_obj.getRules();
        if (rules == null){
            return true;
        }
        setFileNum(10);
        for(TongDunDetailsRule rule: rules) {
            List<TongDunDetailsCondition> conditions = rule.getConditions();
            for(TongDunDetailsCondition condition: conditions) {
                List<TongDunDetailsPartnerHits> hits_for_dim = (condition.getHits_for_dim() == null ? new ArrayList<>()  : condition.getHits_for_dim());
                for(TongDunDetailsPartnerHits hit_dim: hits_for_dim) {
                    TxSb.delete(0, TxSb.length());
                    TxSb.append(formatValue(rule.getRule_id())).append(CRT001);
                    TxSb.append(formatValue(rule.getDecision())).append(CRT001);
                    TxSb.append(formatValue(rule.getScore())).append(CRT001);
                    TxSb.append(formatValue(condition.getType())).append(CRT001);
                    TxSb.append(formatValue(condition.getResult())).append(CRT001);
                    TxSb.append(formatValue(hit_dim.getCount())).append(CRT001);
                    TxSb.append(formatValue(hit_dim.getDim_type())).append(CRT001);
                    TxSb.append(formatValue(hit_dim.getIndustry_display_name())).append(CRT001);
                    TxSb.append(formatValue(hit_dim.getMatch_dim_type())).append(CRT001);
                    TxSb.append(formatValue(hit_dim.getOriginal_dim_type())).append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append("hits_for_dim").append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb + txSb).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }
                List<TongDunDetailsListHits> hits = (condition.getHits() == null ? new ArrayList<>()  : condition.getHits());
                for(TongDunDetailsListHits hit: hits) {
                    TxSb.delete(0, TxSb.length());
                    TxSb.append(formatValue(rule.getRule_id())).append(CRT001);
                    TxSb.append(formatValue(rule.getDecision())).append(CRT001);
                    TxSb.append(formatValue(rule.getScore())).append(CRT001);
                    TxSb.append(formatValue(condition.getType())).append(CRT001);
                    TxSb.append(formatValue(condition.getResult())).append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(formatValue(hit.getCount())).append(CRT001);
                    TxSb.append(formatValue(hit.getEvidence_time())).append(CRT001);
                    TxSb.append(formatValue(hit.getIndustry())).append(CRT001);
                    TxSb.append(formatValue(hit.getIndustry_display_name())).append(CRT001);
                    TxSb.append(formatValue(hit.getOverdue_amount())).append(CRT001);
                    TxSb.append(formatValue(hit.getOverdue_count())).append(CRT001);
                    TxSb.append(formatValue(hit.getOverdue_counts())).append(CRT001);
                    TxSb.append(formatValue(hit.getOverdue_day())).append(CRT001);
                    TxSb.append(formatValue(hit.getOverdue_days())).append(CRT001);
                    TxSb.append(formatValue(hit.getOverdue_time())).append(CRT001);
                    TxSb.append(formatValue(hit.getRepay_status())).append(CRT001);
                    TxSb.append(formatValue(hit.getRisk_type())).append(CRT001);
                    TxSb.append(formatValue(hit.getRole())).append(CRT001);
                    TxSb.append(formatValue(hit.getSubject_type())).append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append("hits").append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb + txSb).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }
                List<TongDunDetailsPartnerCount> dims = (condition.getResults_for_dim() == null ? new ArrayList<>()  : condition.getResults_for_dim());
                for(TongDunDetailsPartnerCount dim: dims) {
                    TxSb.delete(0, TxSb.length());
                    TxSb.append(formatValue(rule.getRule_id())).append(CRT001);
                    TxSb.append(formatValue(rule.getDecision())).append(CRT001);
                    TxSb.append(formatValue(rule.getScore())).append(CRT001);
                    TxSb.append(formatValue(condition.getType())).append(CRT001);
                    TxSb.append(formatValue(condition.getResult())).append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(CRT001);
                    TxSb.append(formatValue(dim.getCount())).append(CRT001);
                    TxSb.append(formatValue(dim.getDim_type())).append(CRT001);
                    TxSb.append(formatValue(dim.getIndustry_display_names())).append(CRT001);
                    TxSb.append(formatValue(dim.getMatch_dim_type())).append(CRT001);
                    TxSb.append(formatValue(dim.getOriginal_dim_type())).append(CRT001);
                    TxSb.append("results_for_dim").append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb + txSb).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }
            }
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tongdun_risk_hit_dim_v2_inc";
    }

    @Override
    public String getSubType() {
        return ".hit_dim";
    }

    @Override
    public String getType() {
        return RecordType.TONGDUN_RISK_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "同盾-命中规则详情查询多头&逾期";
    }
}
